package icu.qx.pro.mq;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.pro.domain.ProSchedule;
import icu.qx.pro.domain.ProWorkOrder;
import icu.qx.pro.domain.ProWorkReport;
import icu.qx.pro.domain.ProWorkTask;
import icu.qx.pro.mapper.ProScheduleMapper;
import icu.qx.pro.mapper.ProWorkOrderMapper;
import icu.qx.pro.mapper.ProWorkTaskMapper;
import icu.qx.pro.service.IProScheduleService;
import icu.qx.pro.service.IProWorkOrderService;
import icu.qx.pro.service.IProWorkReportService;
import icu.qx.pro.service.IProWorkTaskService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 改变状态
 */
@Component
public class ProStatusConsumer {
    @Autowired
    private ProScheduleMapper proScheduleMapper;
    @Autowired
    private ProWorkOrderMapper proWorkOrderMapper;
    @Autowired
    private ProWorkTaskMapper proWorkTaskMapper;

    /**
     * 改变计划的状态 1 已分解 -》 3 进行中
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("doingSchedule.direct"),
            exchange = @Exchange(value = "proStatusConsumer.exchange", type = ExchangeTypes.DIRECT),
            key = {"doingSchedule"}
    ))

    public void doingSchedule(String scheduleCode) {
        // 修改计划状态
        ProWorkOrder condition = new ProWorkOrder();
        condition.setScheduleCode(scheduleCode);
        condition.setWorkOrderStatus("0");
        List<ProWorkOrder> proWorkOrders = proWorkOrderMapper.selectProWorkOrderList(condition);
        // 如果此时没有 0 状态工单，那么改变状态
        if (proWorkOrders == null || proWorkOrders.isEmpty()) {
            // 修改
            ProSchedule proSchedule = new ProSchedule();
            proSchedule.setScheduleCode(scheduleCode);
            proSchedule.setScheduleStatus("3");
            proSchedule.setScheduleRStart(DateUtils.getNowDate());
            proSchedule.setUpdateBy(SecurityUtils.getUsername());
            proSchedule.setUpdateTime(DateUtils.getNowDate());
            proScheduleMapper.updateProSchedule(proSchedule);
        }
    }

    /**
     * 改变工单状态 1 未开始 -》 2
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("changeWorkOrder.direct"),
            exchange = @Exchange(value = "proStatusConsumer.exchange", type = ExchangeTypes.DIRECT),
            key = {"changeWorkOrder"}
    ))
    public void changeWorkOrder(ProWorkOrder proWorkOrder) {
        proWorkOrder.setUpdateBy(SecurityUtils.getUsername());
        proWorkOrder.setUpdateTime(DateUtils.getNowDate());
        proWorkOrderMapper.updateProWorkOrder(proWorkOrder);
    }

    /**
     * 改变任务状态 2 -》 3 && 3 -》4
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("changeWorkTask.direct"),
            exchange = @Exchange(value = "proStatusConsumer.exchange", type = ExchangeTypes.DIRECT),
            key = {"changeWorkTask"}
    ))
    public void changeWorkTask(ProWorkTask proWorkTask) {
        proWorkTask.setUpdateTime(DateUtils.getNowDate());
        proWorkTask.setUpdateBy(SecurityUtils.getUsername());
        proWorkTaskMapper.updateProWorkTask(proWorkTask);
    }

    /**
     * 改变任务状态 已完成
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("completeWorkTask.direct"),
            exchange = @Exchange(value = "proStatusConsumer.exchange", type = ExchangeTypes.DIRECT),
            key = {"completeWorkTask"}
    ))
    public void completeWorkTask(ProWorkTask proWorkTask) {
        proWorkTaskMapper.updateProWorkTask(proWorkTask);
        proWorkTask = proWorkTaskMapper.selectProWorkTaskByWtaskCode(proWorkTask.getWtaskCode());
        completeWorkOrder(proWorkTask.getWtaskWoCode());
    }

    /**
     * 改变工单状态 是否已完成
     */
    public void completeWorkOrder(String workOrderCode) {
        ProWorkTask condition = new ProWorkTask();
        condition.setWtaskWoCode(workOrderCode);
        condition.setWtaskStatus("6");
        List<ProWorkTask> proWorkTasks = proWorkTaskMapper.selectProWorkTaskList(condition);
        condition.setWtaskStatus(null);
        List<ProWorkTask> totalTasks = proWorkTaskMapper.selectProWorkTaskList(condition);
        BigDecimal totalNum = proWorkTasks.stream().map(ProWorkTask::getWtaskRNum).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 修改
        ProWorkOrder proWorkOrder = new ProWorkOrder();
        proWorkOrder.setWorkOrderCode(workOrderCode);
        proWorkOrder.setUpdateBy(SecurityUtils.getUsername());
        proWorkOrder.setUpdateTime(DateUtils.getNowDate());
        proWorkOrder.setWorkOrderProgress(BigDecimal.valueOf(proWorkTasks.size()).divide(BigDecimal.valueOf(totalTasks.size()), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
        proWorkOrder.setWorkOrderComplete(totalNum);
        if (proWorkTasks.size() == totalTasks.size()) {
            proWorkOrder.setWorkOrderStatus("3");
            proWorkOrder.setWorkOrderREnd(DateUtils.getNowDate());
            proWorkOrderMapper.updateProWorkOrder(proWorkOrder);
            proWorkOrder = proWorkOrderMapper.selectProWorkOrderByWorkOrderCode(workOrderCode);
            completeSchedule(proWorkOrder.getScheduleCode());
        } else {
            proWorkOrderMapper.updateProWorkOrder(proWorkOrder);
        }
    }

    /**
     * 改变计划的状态 3 进行中 -》 5 已完成
     */
    public void completeSchedule(String scheduleCode) {
        // 修改计划状态
        ProWorkOrder condition = new ProWorkOrder();
        condition.setScheduleCode(scheduleCode);
        condition.setWorkOrderStatus("3");
        List<ProWorkOrder> proWorkOrders = proWorkOrderMapper.selectProWorkOrderList(condition);
        condition.setWorkOrderStatus(null);
        List<ProWorkOrder> totalOrders = proWorkOrderMapper.selectProWorkOrderList(condition);
        BigDecimal totalNum = proWorkOrders.stream().map(ProWorkOrder::getWorkOrderComplete).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 如果此时总的工单数量和计划数量相同，那么改变状态
        ProSchedule proSchedule = new ProSchedule();
        proSchedule.setScheduleRNum(totalNum);
        if (proWorkOrders != null && proWorkOrders.size() == totalOrders.size()) {
            // 修改
            proSchedule.setScheduleCode(scheduleCode);
            proSchedule.setScheduleStatus("5");
            proSchedule.setScheduleRComple(DateUtils.getNowDate());
            proSchedule.setUpdateBy(SecurityUtils.getUsername());
            proSchedule.setUpdateTime(DateUtils.getNowDate());
            proScheduleMapper.updateProSchedule(proSchedule);
        } else {
            proScheduleMapper.updateProSchedule(proSchedule);
        }
    }
}
